package com.mapapp.flutter_mapapp.persenter;

import android.content.Context;
import android.util.Log;

import com.mapapp.flutter_mapapp.bean.AvatarBean;
import com.mapapp.flutter_mapapp.bean.LogEntity;
import com.mapapp.flutter_mapapp.http.ActivityLifeCycleEvent;
import com.mapapp.flutter_mapapp.http.Api;
import com.mapapp.flutter_mapapp.http.HttpUtil;
import com.mapapp.flutter_mapapp.http.ProgressSubscriber;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.MineViewInterface;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.subjects.PublishSubject;

public class MinePresenter {
    private Context mContext;
    private MineViewInterface mView;
    private PublishSubject<ActivityLifeCycleEvent> lifecycleSubject;

    public MinePresenter(MineViewInterface view, Context context, PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        this.mView = view;
        this.mContext = context;
        this.lifecycleSubject = lifecycleSubject;
    }

    public void uploadAvatar(String filePath) {
        File file = new File(filePath);
        Log.e("Test", "" + file.exists());
//        MultipartBody.Builder builder = new MultipartBody.Builder()
//                .setType(MultipartBody.FORM);
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//        builder.addFormDataPart("file", file.getName(), imageBody);

        MultipartBody.Part body =
                MultipartBody.Part.createFormData("file", file.getName(), imageBody);


        HttpUtil.getInstance().toSubscribe(Api.getApiService().uploadAvatar(body),
                new ProgressSubscriber<String>(mContext, false) {
                    @Override
                    protected void _onNext(String s) {
                        mView.showResult(true);
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.showMessage(mContext, message);
                        mView.showResult(false);
                    }
                }, lifecycleSubject);
    }

    public void getAvatar() {
        HttpUtil.getInstance().toSubscribe(Api.getApiService().getAppAvatar(),
                new ProgressSubscriber<AvatarBean>(mContext, false) {
                    @Override
                    protected void _onNext(AvatarBean s) {
                        mView.showAvatarInfo(s);
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.showMessage(mContext, message);
                    }
                }, lifecycleSubject);
    }

    public void submitOperateInfo(String type, String regionId, String processId, String fixType, final LogEntity entity) {
        HttpUtil.getInstance().toSubscribeNoDialog(Api.getApiService().submitOperateInfo(type, regionId, processId, fixType),
                new ProgressSubscriber<Object>(mContext, false) {
                    @Override
                    protected void _onNext(Object s) {
                        mView.uploadLog(true, entity);
                    }

                    @Override
                    protected void _onError(String message) {
                        mView.uploadLog(false, entity);
                    }
                }, lifecycleSubject);
    }


}
